//
//  NetMyGoUClient.m
//  NetTest
//
//  Created by uistrong on 13-3-20.
//  Copyright (c) 2013年 uistrong. All rights reserved.
//

#import "NetMyGoUClient.h"
#include <arpa/inet.h>
#include "Communicator.h"
#include "GUNetDef.h"

static NetMyGoUClient *s_client= nil;

@implementation NetMyGoUClient

- (int) simpleCommunicate:(NSMutableData*) sendRecvData timeOut:(int) timeOut{
    
    Communicator communicator;

    int nRet = communicator.Connect(_ipAddr.UTF8String, _port);
    if (nRet != SUCCESS) {
        NSLog(@"连接失败：ret: %d", nRet);
        communicator.DisConnect();
        return nRet;
    }
    
    // 发送数据
    nRet = communicator.SendData((char*)[sendRecvData bytes], [sendRecvData length]);
    if (nRet != SUCCESS) {
        NSLog(@"发送失败：ret: %d", nRet);
        communicator.DisConnect();
        return nRet;
    }
    
    // 先接收头，再接收数据
    int nHeaderLen = sizeof(t_net_header);
    [sendRecvData setLength:nHeaderLen]; // setLength
    char *buffer = (char*)[sendRecvData mutableBytes];
    
    nRet = communicator.RecvLength(buffer, nHeaderLen, nHeaderLen);
    
    // 获取数据长度
    t_net_header *header = (t_net_header*)buffer;
    if (header->head4[0] != NET_Header_ID) {    // 校验
        communicator.DisConnect();
        return NET_DATA_ERROR;
    }
    
    // 转化为小端
    header->dataLen = ntohl(header->dataLen);
    // 接收数据
    int nDataLen = header->dataLen + 8; // 最后有8个字节的crc
    
    [sendRecvData setLength:nDataLen]; // setLength
    buffer = (char*)[sendRecvData mutableBytes];
    
    nRet = communicator.RecvLength(buffer, nDataLen, nDataLen);
    if (nRet != SUCCESS) {
        communicator.DisConnect();
        return nRet;
    }
    
    // 关闭连接
    communicator.DisConnect();
    return SUCCESS;
}

- (int) simpleCommunicate:(char*) sendRecvBuffer dataLength:(int&) dataLength bufferLength:(int) bufferLength timeOut:(int) timeOut
{
    Communicator communicator;
    
    int nRet = communicator.Connect(_ipAddr.UTF8String, _port);
    if (nRet != SUCCESS) {
        NSLog(@"连接失败：ret: %d", nRet);
        communicator.DisConnect();
        return nRet;
    }
    
    // 发送数据
    nRet = communicator.SendData(sendRecvBuffer, dataLength);
    if (nRet != SUCCESS) {
        NSLog(@"发送失败：ret: %d", nRet);
        communicator.DisConnect();
        return nRet;
    }
    
    // 先接收头，再接收数据
    int nHeaderLen = sizeof(t_net_header);
    nRet = communicator.RecvLength(sendRecvBuffer, bufferLength, nHeaderLen);
    
    // 获取数据长度
    t_net_header *header = (t_net_header*)sendRecvBuffer;
    if (header->head4[0] != NET_Header_ID) {    // 校验
        communicator.DisConnect();
        return NET_DATA_ERROR;
    }
    
    // 转化为小端
    header->dataLen = ntohl(header->dataLen);
    
    // 接收数据
    int nDataLen = header->dataLen + 8; // 最后有8个字节的crc
    
    nRet = communicator.RecvLength(sendRecvBuffer, bufferLength, nDataLen);
    if (nRet != SUCCESS) {
        communicator.DisConnect();
        return nRet;
    }
    
    // 关闭连接
    communicator.DisConnect();
    
    dataLength = nDataLen;
    return SUCCESS;
}


+ (NetMyGoUClient *)sharedInterface
{
    if (s_client == nil) {
        s_client = [[NetMyGoUClient alloc] init];
    }
    return s_client;
}

- (void) showWait{
    if (_processAlertView == nil) {
        _processAlertView = [[UIAlertView alloc]
                             initWithTitle:@"正在连接..."
                             message:nil
                             delegate:nil
                             cancelButtonTitle:nil
                             otherButtonTitles:nil];
        UIActivityIndicatorView *Activity=[[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(130, 65, 20, 20)];
        [Activity startAnimating];
        [_processAlertView addSubview:Activity];
    }
    [_processAlertView show];
}

- (void) hiddWait{
    if (_processAlertView != nil) {
        [_processAlertView dismissWithClickedButtonIndex:0 animated:YES];
    }
}
@end


